<template>
  <div class="new-address">
    <div class="plus"></div>
    <van-nav-bar :title="aid === undefined ? '新增地址' : '编辑地址'" left-text="返回" left-arrow @click-left="goBack" />
    <van-address-edit :area-list="areaList" show-postal :show-delete="!!aid" show-set-default :area-columns-placeholder="['请选择省', '请选择市', '请选择区/县']" @save="onSave" @delete="onDelete" :address-info="getAddressData" />
  </div>
</template>

<script>
import { areaList } from "@vant/area-data";

export default {
  name: "NewAddress",

  data() {
    return {
      areaList,
      aid: null,
      getAddressData: {},
    };
  },

  created() {
    // 获取路由传递的aid
    this.aid = this.$route.params.aid;

    if (this.aid !== undefined) {
      this.getAddress(this.aid);
    }
  },

  methods: {
    goBack() {
      this.$router.go(-1);
    },

    // 保存添加地址
    addAddress(content) {
      // 将地址表单的数据解构赋值给data
      let data = { ...content };
      // 将默认地址选项转换成数字
      data.isDefault = Number(data.isDefault);
      // 然后删除国家选项
      delete data.country;
      // 
      let tokenString = this.$cookies.get("TOKEN");
      // 发送添加地址请求
      this.axios
        .post("addAddress", {
          tokenString,
          ...data,
        })
        .then((res) => {
          // 
          if (res.data.code == 700) {
            this.$toast("请先登录");
            // 清除token
            this.$cookies.remove("TOKEN");
            // 延迟2秒后跳转到登录页面
            let timer = setTimeout(() => {
              clearTimeout(timer);
              this.$router.replace({ name: "Login" });
            }, 2000);
            return;
          }
          if (res.data.code === 9000) {
            this.$toast(res.data.msg);
            // 延迟1秒后跳转到地址列表页面
            let timer = setTimeout(() => {
              clearTimeout(timer);
              this.$router.push({ name: "Address" });
            }, 1000);
            return;
          }
        })
        .catch((err) => {
          
        });
    },

    // 查询编辑地址
    getAddress(aid) {
      let tokenString = this.$cookies.get("TOKEN");
      this.axios
        .get("findAddressByAid", {
          params: {
            tokenString,
            aid,
          },
        })
        .then((res) => {
          // 
          if (res.data.code == 700) {
            this.$toast("请先登录");
            // 清除token
            this.$cookies.remove("TOKEN");
            // 延迟2秒后跳转到登录页面
            let timer = setTimeout(() => {
              clearTimeout(timer);
              this.$router.replace({ name: "Login" });
            }, 2000);
            return;
          }

          if (res.data.code === 40000) {
            // 将默认选择从数字转换成布尔值
            res.data.result[0].isDefault = Boolean(
              res.data.result[0].isDefault
            );
            this.getAddressData = res.data.result[0];
          }
        })
        .catch((err) => {
          
        });
    },

    // 点击保存按钮时
    onSave(content) {
      let tokenString = this.$cookies.get("TOKEN");
      // 将地址表单的数据解构赋值给data
      let data = { ...content };
      // 将默认地址选项转换成数字
      data.isDefault = Number(data.isDefault);
      // 然后删除国家选项
      delete data.country;
      let url = "";
      // 判断是否处于编辑状态
      if (this.aid !== undefined) {
        // 这里是编辑地址
        url = "editAddress";
        // 编辑地址需要传递aid
        data.aid = this.aid;
      } else {
        // 这里是新增地址
        url = "addAddress";
      }
      this.axios
        .post(url, {
          tokenString,
          ...data,
        })
        .then((res) => {
          // 
          if (res.data.code == 700) {
            this.$toast("请先登录");
            // 清除token
            this.$cookies.remove("TOKEN");
            // 延迟2秒后跳转到登录页面
            let timer = setTimeout(() => {
              clearTimeout(timer);
              this.$router.replace({ name: "Login" });
            }, 2000);
            return;
          }
          // 新增地址成功
          if (res.data.code === 9000) {
            this.$toast(res.data.msg);
          }
          // 编辑地址成功
          if (res.data.code === 30000) {
            this.$toast(res.data.msg);
          }

          // 延迟1秒后返回上一级页面
          let timer = setTimeout(() => {
            clearTimeout(timer);
            this.$router.go(-1);
          }, 1000);
        })
        .catch((err) => {
          
        });
    },

    // 点击删除按钮时
    onDelete(content) {
      let tokenString = this.$cookies.get("TOKEN");
      this.axios
        .post("deleteAddress", {
          tokenString,
          aid: content.aid,
        })
        .then((res) => {
          // 
          if (res.data.code == 700) {
            this.$toast("请先登录");
            // 清除token
            this.$cookies.remove("TOKEN");
            // 延迟2秒后跳转到登录页面
            let timer = setTimeout(() => {
              clearTimeout(timer);
              this.$router.replace({ name: "Login" });
            }, 2000);
            return;
          }
          // 新增地址成功
          if (res.data.code === 10000) {
            this.$toast(res.data.msg);
            // 延迟1秒后跳转到地址列表页面
            let timer = setTimeout(() => {
              clearTimeout(timer);
              this.$router.push({ name: "Address" });
            }, 1000);
            return;
          }
        })
        .catch((err) => {
          
        });
    },
  },
};
</script>

<style lang="less" scoped>
.van-nav-bar {
  /deep/.van-icon {
    color: #ffa341;
  }
  /deep/.van-nav-bar__text {
    color: #ffa341;
  }
  /deep/.van-nav-bar__title {
    color: #333;
  }
}
/deep/ .van-switch--on {
  background-color: #ff594d;
}
/deep/ .van-button--danger {
  background-color: #ff594d;
  border-color: #ff594d;
}
</style>